package com.yksj.healthtalk.ui.setting;

import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;
/**
 * 意见反馈
 * @author Administrator
 *
 */
public class SettingFeedbackUI extends BaseFragmentActivity implements OnClickListener {
	private EditText mEditText;
	private int textNumber = 0;
	TextView textNum = null;
	IntentFilter filter_faceback;//广播注册
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		
		setContentView(R.layout.setting_layout_feedback);
		
		initView();
	}
	
	private void initView() {
		
		initTitle();
		
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("提交");
		titleTextV.setText("意见反馈");
		
		
		mEditText = (EditText)findViewById(R.id.setting_feedback_text);
		mEditText.addTextChangedListener(textWatcher);
		textNum = (TextView)findViewById(R.id.textcount);
		textNum.setText("0/500");
		filter_faceback = new IntentFilter();    
		filter_faceback.addAction("com.yksj.ui.ACTION_SEND_FACEBACK");
	}

	/**
	 * 文字监听
	 */
	private TextWatcher textWatcher = new TextWatcher() {  
        @Override    
        public void afterTextChanged(Editable s) { 
        }   
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count,  
                int after) {  
        }  
         @Override    
        public void onTextChanged(CharSequence s, int start, int before,     
                int count) {   
        	 if((start +count)<=500 && mEditText.getText().toString().length()<=500){
        		 textNumber = mEditText.getText().toString().length();
        		 textNum.setText(textNumber+"/500"); 
        	 }else{
        		 mEditText.setText(s.subSequence(0, 500));
        		 ToastUtil.showShort(SettingFeedbackUI.this, R.string.most_five_hundred);
        	 }
        }                    
    };
    
    
	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_back:
				SystemUtils.hideSoftBord(this,mEditText);
				finish();
				overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
				break;
				
			case R.id.title_right2:
				submitFeedback();
				break;
			}
		}
	
	/**
	 * 提交意见内容
	 */
	private void submitFeedback(){
		WheelUtils.hideInput(SettingFeedbackUI.this,mEditText.getWindowToken());
		String value = mEditText.getEditableText().toString().trim();
		if("".equals(value)){
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), getString(R.string.opinion_null));
			return;
		}
		
		if(!SystemUtils.isNetWorkValid(this)){
			ToastUtil.showShort(this, R.string.getway_error_note);
			return;
		}
		HttpRestClient.dohttpFeedBack(value ,
				new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(JSONObject response) {
				super.onSuccess(response);
				if(response.containsKey("error_code")){
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  response.getString("error_message"));
				}else{
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  response.getString("info"),new OnClickSureBtnListener() {
						@Override
						public void onClickSureHander() {
							SettingFeedbackUI.this.finish();
						}
					});
				}
			}
			
		});
	}
	
	

}
